
/**
 * @version     $Id: adminsafe.js 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

function changeBanStatus(id){

    //  Make sure current status is not error
    if ( $(id + '_status').value == "-1"){
        return;
    }

    //  Prepare waiter
    var waiter = $(id + '_waiter');
    waiter.fade('hide');
    waiter.addClass("ajax-loading");
    waiter.style.visibility = '';
    waiter.fade('show');
    
    //
    var ip =  $(id + '_ip').value;
    var action = ($(id + '_status').value == 0) ? 'ban' : 'unban';
    var img =  (action=='ban') ? $('banImg').value : $('unbanImg').value;
    var title = (action=='ban') ? $('clickToUnBanText').value : $('clickToBanText').value;
    var status = (action=='ban') ? 1 : 0;
    
    new Ajax($('ajaxUrl').value, {
        method: 'post',
        data: {
            controller : 'ipblacklist',
            task : action,
            ip : ip
        },
        onComplete: function(response){
            if (response == "1"){
                //  Success
                updateAllIps(ip, img, title, status);
            }else if (response == "0"){
                //  Error
                updateAllIps(ip, img, 'Failed', -1);
            }else{
                //  Warnign
                updateAllIps(ip, $('warningImg').value, response, -1);
                alert(response);
            }
            //
            waiter.fade('out');
        },
        onError: function(){
            updateAllIps(ip, $('errorImg').value, 'Error', -1);
            waiter.fade('out');
        }
    }).request();
}


/**
 * Mark All
 */
function updateAllIps(ipAddress, img, title, status){

    //  Find all ID Holders
    var items = $('tablecell').getElements('input[name=id]');
    //  Scan through all items
    items.each(function(input){
        var id = input.value;
        //  Get IP
        var ip = $(id + '_ip').value;
        if ($chk(ip)){
            //  Check if IP Match
            if (ip == ipAddress){
                //  Change Link and Img if they do
                $(id + '_img').src =  img;
                $(id + '_link').title = title
                $(id + '_status').value =  status;
            }
        }
    });

}

